'use strict';
import { config } from 'config.js';

let host = config().host;

export function get(url, data){
      var data = data || {};
      if(!data['wxa_code']){
        data['wxa_session_id'] = wx.getStorageSync('wxa_session_id');
      }
      // host = 'http://127.0.0.1:9002/'
      return new Promise(function(resolve, reject){

         wx.request({
             url: host+url.path,
             header: {
                 'Content-Type': 'application/json'
             },
             data: data,
             success: function(res) {
              if(res.data.ret == 0){
                resolve(res)
              }else{
                resolve(res)
              }    
             },
             fail: function(res) {
               reject(res)
             }
         })
      })
    }

export function post(url, data){
      var data = data || {};
      var wholeUrl = host + url.path + '?wxa_session_id=' + wx.getStorageSync('wxa_session_id');
      return new Promise(function(resolve, reject){
        wx.request({
            url:  wholeUrl,
            data: data,
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded' // 默认值
            },
            success: function(res) {
                if(res.data.ret == 0){
                  resolve(res)
                }else{
                  resolve(res)
                }    
            },
            fail: function(res) {
                reject(res)
            }
        });
      })
      
    }